package com.qiya.LiaoDa;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.qiya.LiaoDa.Util.MySqLiteHelper;

public class ResterActivity extends AppCompatActivity {

    MySqLiteHelper helper = null;
    private EditText register_user, register_password;
    private Button register_suer;
    @Override
    protected void onCreate (Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rester);
        //创建数据库
        helper = new MySqLiteHelper(this, "SAVE_USER.db", null, 1);
        SQLiteDatabase database = helper.getReadableDatabase();
        initViews();

    }


    private void initViews () {
        register_user = findViewById(R.id.edit_login_rester);
        register_password = findViewById(R.id.edit_pwd_rester);
        register_suer = findViewById(R.id.btn_zhuche_rester);
        //点击确定按钮返回登录界面
        register_suer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //trim()去空格
                String RegisterUser = register_user.getText().toString().trim();
                String RegisterPassword = register_password.getText().toString().trim();
                //判断输入的账号密码是否为空，是则提示用户输入正确的
                if (RegisterUser.equals("") && RegisterPassword.equals("")) {
                    Toast.makeText(ResterActivity.this, "请输入学号/密码", Toast.LENGTH_LONG).show();
                } else {
                    //账号密码有效，执行下一步
                    /**
                     * 把获取的数据存储到数据库
                     * 1.insert方法添加数据到数据库
                     */
                    SQLiteDatabase database = helper.getReadableDatabase();
                    ContentValues values = new ContentValues();
                    //键值对的形式
                    values.put("name", RegisterUser);
                    values.put("password", RegisterPassword);
                    //添加到user表
                    database.insert("user", null, values);
                    Toast.makeText(ResterActivity.this, "恭喜你，注册成功！", Toast.LENGTH_LONG).show();
                    //账号密码验证正确，显示意图跳转到登录界面
                    //Toast.makeText(ResterActivity.this, "恭喜你，注册成功！", Toast.LENGTH_LONG).show();
                    Intent intent;
                    intent = new Intent(ResterActivity.this, LoginActivity.class);
                    startActivity(intent);
                }
//                if (RegisterUser.length() != 11){
//                    Toast.makeText(ResterActivity.this, "请输入正确手机号", Toast.LENGTH_LONG).show();
//                }
            }
        });
    }
}
